package com.etc.controller;

import com.etc.dto.DrugDto;
import com.etc.dto.Result;
import com.etc.entity.History;
import com.etc.service.DetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/detail")
public class DetailController {

    private static final Logger logger = LoggerFactory.getLogger(DetailController.class);


    @Autowired
    private DetailService detailService;

    @PostMapping("/drug/{orderId}")
    public @ResponseBody Result drug(@RequestBody List<DrugDto> drugDtoList,@PathVariable String orderId) {
        Result result = new Result();

        StringBuffer content = new StringBuffer();
        for (DrugDto drugDto : drugDtoList) {

            if (drugDto.getDrugName().contains("胶囊")) {
                content.append(drugDto.getNumber()).append("盒").append(drugDto.getDrugName()).append(":").append(drugDto.getContent()).append(",");
            } else if (drugDto.getDrugName().contains("片")) {
                content.append(drugDto.getNumber()).append("盒").append(drugDto.getDrugName()).append(":").append(drugDto.getContent()).append(",");
            }  else if (drugDto.getDrugName().contains("丸")) {
                content.append(drugDto.getNumber()).append("瓶").append(drugDto.getDrugName()).append(":").append(drugDto.getContent()).append(",");
            } else if (drugDto.getDrugName().contains("膏")) {
                content.append(drugDto.getNumber()).append("瓶").append(drugDto.getDrugName()).append(":").append(drugDto.getContent()).append(",");
            }
        }
        logger.debug(content.toString());
        boolean flag = detailService.addDetail(content.toString(),orderId);
        if (flag){
            return result.ok(flag);
        }else {
            return result.fail(0,"添加失败");
        }
    }
    @GetMapping("/delete")
    public @ResponseBody
    Result getHistoryDetail(@RequestParam String patientId) {
        Result result = new Result();
        boolean falg = detailService.deleteDetail(patientId);
        if (falg) {
            return result.ok(true);
        } else {
            return result.fail(0, "删除失败");
        }
    }
}
